package com.tianyang.yukuaifu.rxservice.client;

import android.text.TextUtils;

import androidx.core.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.auth.bean.AreaCityOutput;
import com.tianyang.yukuaifu.business.auth.bean.BankRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifu.business.auth.bean.LicenseRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.MerchantVerifyOutput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.login.bean.LoginOutput;
import com.tianyang.yukuaifu.business.main.bean.AppVersionOutput;
import com.tianyang.yukuaifu.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifu.business.main.bean.HelpCenterOutput;
import com.tianyang.yukuaifu.business.main.bean.PosAgentOutput;
import com.tianyang.yukuaifu.business.main.bean.PosRateOutput;
import com.tianyang.yukuaifu.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifu.business.main.bean.UserCenterOutput;
import com.tianyang.yukuaifu.business.trade.bean.AchievementOutput;
import com.tianyang.yukuaifu.business.trade.bean.MerchantAchieveOutput;
import com.tianyang.yukuaifu.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifu.business.trade.bean.SubAgentStatOutput;
import com.tianyang.yukuaifu.common.interfaces.UrlConstants;
import com.tianyang.yukuaifu.common.utils.CommonViewUtils;
import com.tianyang.yukuaifu.rxservice.BaseClient;
import com.tianyang.yukuaifu.rxservice.MetaBean;
import com.tianyang.yukuaifu.rxservice.RxError;
import com.tianyang.yukuaifu.rxservice.interfaces.PlateAgentService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：plate-agent接口
 * Created by xyj on 2024/3/12
 */
public class PlateAgentClient extends BaseClient {

    private static PlateAgentClient mInstance;

    public static PlateAgentClient getInstance() {
        if (mInstance == null) {
            synchronized (PlateAgentClient.class) {
                if (mInstance == null) {
                    mInstance = new PlateAgentClient();
                }
            }
        }
        return mInstance;
    }

    private PlateAgentService service;

    private PlateAgentClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.BASE_AGENT_URL)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(PlateAgentService.class);
    }

    /**
     * 登录接口
     *
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> merchantLogin(Map<String, String> reqData) {
        return callMap(service.merchantLogin(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), LoginOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 登录接口
     *
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> agentLogin(Map<String, String> reqData) {
        return callMap(service.agentLogin(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), LoginOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 手机验证码登录
     *
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> merchantMobileLogin(Map<String, String> reqData) {
        return callMap(service.merchantMobileLogin(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), LoginOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 手机验证码登录
     *
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> agentMobileLogin(Map<String, String> reqData) {
        return callMap(service.agentMobileLogin(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), LoginOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 注册接口
     *
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> register(Map<String, String> reqData) {
        return callMap(service.register(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), LoginOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取短信验证码
     */
    public Observable<JSONObject> sendSms(Map<String, String> reqData) {
        return callMap(service.getVerifyCode(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改密码
     * @param reqData 请求参数
     */
    public Observable<JSONObject> modifyPassword(Map<String, String> reqData) {
        return callMap(service.modifyPassword(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 忘记密码
     * @param reqData 请求参数
     */
    public Observable<JSONObject> forgetPassword(Map<String, String> reqData) {
        return callMap(service.forgetPassword(reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 获取用户信息
     */
    public Observable<UserCenterOutput> getUserInfo() {
        return callMap(service.getUserInfo(CommonViewUtils.getAuthorization(), new HashMap<>()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<UserCenterOutput>>() {
                    @Override
                    public Observable<UserCenterOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        UserCenterOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), UserCenterOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 保存进件临时数据
     */
    public Observable<JSONObject> saveIdCertTemp(Map<String, String> reqData) {
        return callMap(service.saveIdCertTemp(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 查询进件临时数据
     */
    public Observable<JSONObject> getIdCertTemp(Map<String, String> reqData) {
        return callMap(service.getIdCertTemp(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 查询行业MCC   所属行业
     */
    public Observable<List<BusinessOutput>> posMccCode(Map<String, String> reqData) {
        return callMapList(service.posMccCode(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<BusinessOutput>>>() {
                    @Override
                    public Observable<List<BusinessOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<BusinessOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<BusinessOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 查询MCC  支付宝 微信
     */
    public Observable<List<BusinessOutput>> yplMccCode(Map<String, String> reqData) {
        return callMapList(service.yplMccCode(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<BusinessOutput>>>() {
                    @Override
                    public Observable<List<BusinessOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<BusinessOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<BusinessOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 查询银行卡银联号
     */
    public Observable<List<BusinessOutput>> posBankBranchCode(Map<String, String> reqData) {
        return callMapList(service.posBankBranchCode(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<BusinessOutput>>>() {
                    @Override
                    public Observable<List<BusinessOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<BusinessOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<BusinessOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 身份正面ORC识别
     */
    public Observable<IdCardOutput> idCardReg(Map<String, String> reqData) {
        return callMap(service.idCardReg(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<IdCardOutput>>() {
                    @Override
                    public Observable<IdCardOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        IdCardOutput output = null;
                        if (pair.second != null && pair.second.containsKey("result")) {
                            Map<String, Object> resultData = JSON.parseObject(JSON.toJSONString(pair.second.get("result")),
                                    new TypeReference<Map<String, Object>>() {
                                    });
                            if (resultData != null && resultData.containsKey("result")) {
                                output = JSON.parseObject(JSON.toJSONString(resultData.get("result")), new TypeReference<IdCardOutput>() {
                                });
                                if (output == null) output = new IdCardOutput();
                                output.setCodeType(resultData.get("code") + "");
                                output.setMsg(resultData.get("msg") + "");
                            }
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 银行卡ORC
     */
    public Observable<BankRegOutput> bankReg(Map<String, String> reqData) {
        return callMap(service.bankReg(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<BankRegOutput>>() {
                    @Override
                    public Observable<BankRegOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        BankRegOutput output = null;
                        if (pair.second != null && pair.second.containsKey("result")) {
                            Map<String, Object> resultData = JSON.parseObject(JSON.toJSONString(pair.second.get("result")),
                                    new TypeReference<Map<String, Object>>() {
                                    });
                            if (resultData != null && resultData.containsKey("result")) {
                                output = JSON.parseObject(JSON.toJSONString(resultData.get("result")), new TypeReference<BankRegOutput>() {
                                });
                            }
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 营业执照识别
     */
    public Observable<LicenseRegOutput> licenseReg(Map<String, String> reqData) {
        return callMap(service.licenseReg(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LicenseRegOutput>>() {
                    @Override
                    public Observable<LicenseRegOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LicenseRegOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<LicenseRegOutput>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 省份
     */
    public Observable<List<AreaCityOutput>> getProvinceList(Map<String, String> reqData) {
        return callMapList(service.getProvinceList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<AreaCityOutput>>>() {
                    @Override
                    public Observable<List<AreaCityOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<AreaCityOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<AreaCityOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 市
     */
    public Observable<List<AreaCityOutput>> getCityList(Map<String, String> reqData) {
        return callMapList(service.getCityList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<AreaCityOutput>>>() {
                    @Override
                    public Observable<List<AreaCityOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<AreaCityOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<AreaCityOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 地区
     */
    public Observable<List<AreaCityOutput>> getAreaList(Map<String, String> reqData) {
        return callMapList(service.getAreaList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<AreaCityOutput>>>() {
                    @Override
                    public Observable<List<AreaCityOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<AreaCityOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<AreaCityOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 服务商实名认证
     */
    public Observable<JSONObject> sendIdCertByService(Map<String, String> reqData) {
        return callMap(service.idcert3(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null || pair.first == null) {
                            return Observable.error(RxError.create(RxError.ERROR_UNKNOWN, RxError.ERROR_UNKNOWN));
                        }
                        if (pair.first.getCode() == 200 || pair.first.getCode() == 402) {
                            return Observable.just(pair.second);
                        }
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 实名认证
     */
    public Observable<JSONObject> sendIdCert(Map<String, String> reqData) {
        return callMap(service.idcert3(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 查询费率模板
     */
    public Observable<List<PosRateOutput>> getPosTemplateRate(Map<String, Object> reqData) {
        return callMapList(service.getPosTemplateRate(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<PosRateOutput>>>() {
                    @Override
                    public Observable<List<PosRateOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<PosRateOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<PosRateOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 费率设置
     */
    public Observable<JSONObject> addPosUserRate(Map<String, Object> reqData) {
        return callMap(service.addPosUserRate(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 发展商户列表
     */
    public Observable<List<PosUserOutput>> posUserManage(Map<String, String> reqData) {
        return callMapList(service.posUserManage(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<PosUserOutput>>>() {
                    @Override
                    public Observable<List<PosUserOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<PosUserOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<PosUserOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 服务商列表
     */
    public Observable<List<PosAgentOutput>> posAgentManage(Map<String, String> reqData) {
        return callMapList(service.posAgentManage(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<PosAgentOutput>>>() {
                    @Override
                    public Observable<List<PosAgentOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<PosAgentOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<PosAgentOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 查询审核状态
     */
    public Observable<JSONObject> getIdcertconfirm(Map<String, Object> reqData) {
        return callMap(service.getIdcertconfirm(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 授权
     */
    public Observable<MerchantVerifyOutput> yplMerchantVerify(Map<String, String> reqData) {
        return callMap(service.yplMerchantVerify(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<MerchantVerifyOutput>>() {
                    @Override
                    public Observable<MerchantVerifyOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        MerchantVerifyOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<MerchantVerifyOutput>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 新增店铺
     */
    public Observable<JSONObject> createNewStoreMerchant(Map<String, String> reqData) {
        return callMap(service.createNewStoreMerchant(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 合成海报
     */
    public Observable<JSONObject> compositePoster(Map<String, Object> reqData) {
        return callMap(service.compositePoster(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 商户结算卡查询
     *
     * @param reqData 请求参数
     */
    public Observable<SettleBankOutput> queryUserSettlementCard(Map<String, String> reqData) {
        return callMap(service.queryjiesuancard(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<SettleBankOutput>>() {
                    @Override
                    public Observable<SettleBankOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        SettleBankOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString()) && pair.second.containsKey("data")) {
                            JSONArray jsonArray = pair.second.getJSONArray("data");
                            if (jsonArray != null) {
                                output = JSON.toJavaObject(jsonArray.getJSONObject(0), SettleBankOutput.class);
                            }
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 首页统计接口
     *
     * @param reqData 请求参数
     */
    public Observable<JSONObject> appAgentHomeStatQb(Map<String, Object> reqData) {
        return callMap(service.appAgentHomeStatQb(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 首页统计接口
     *
     * @param reqData 请求参数
     */
    public Observable<AchievementOutput> getAgentStatHistory(Map<String, String> reqData) {
        return callMap(service.getAgentStatHistory(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<AchievementOutput>>() {
                    @Override
                    public Observable<AchievementOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        AchievementOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), AchievementOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 服务商交易统计
     *
     * @param reqData 请求参数
     */
    public Observable<List<MerchantAchieveOutput>> posMerchantTradeTotal(Map<String, Object> reqData) {
        return callMapList(service.posMerchantTradeTotal(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<MerchantAchieveOutput>>>() {
                    @Override
                    public Observable<List<MerchantAchieveOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MerchantAchieveOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<MerchantAchieveOutput>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 直推商户详情
     *
     * @param reqData 请求参数
     */
    public Observable<JSONObject> posMerchantTradeDetail(Map<String, Object> reqData) {
        return callMap(service.posMerchantTradeDetail(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 下级服务商业绩
     *
     * @param reqData 请求参数
     */
    public Observable<List<MerchantAchieveOutput>> subAgentStatHistoryList(Map<String, Object> reqData) {
        return callMapList(service.subAgentStatHistoryList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<MerchantAchieveOutput>>>() {
                    @Override
                    public Observable<List<MerchantAchieveOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<MerchantAchieveOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<MerchantAchieveOutput>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 间推业绩详情
     *
     * @param reqData 请求参数
     */
    public Observable<SubAgentStatOutput> subAgentStatHistoryDetail(Map<String, Object> reqData) {
        return callMapList(service.subAgentStatHistoryDetail(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<SubAgentStatOutput>>() {
                    @Override
                    public Observable<SubAgentStatOutput> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        SubAgentStatOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), SubAgentStatOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 帮助中心
     */
    public Observable<HelpCenterOutput> getServicePhone() {
        return callMap(service.getServicePhone(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<HelpCenterOutput>>() {
                    @Override
                    public Observable<HelpCenterOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        HelpCenterOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), HelpCenterOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取示例图片
     */
    public Observable<SampleImageOutput> getSampleImage() {
        return callMap(service.getSampleImage(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<SampleImageOutput>>() {
                    @Override
                    public Observable<SampleImageOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        SampleImageOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = pair.second.toJavaObject(SampleImageOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 获取APP版本信息
     */
    public Observable<AppVersionOutput> getAppVersionInfo() {
        return callMap(service.getAppVersionInfo(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<AppVersionOutput>>() {
                    @Override
                    public Observable<AppVersionOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        AppVersionOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = pair.second.toJavaObject(AppVersionOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 经营范围
     */
    public Observable<List<String>> getBusinessScopeList() {
        return callMapList(service.getBusinessScopeList(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<String>>>() {
                    @Override
                    public Observable<List<String>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<String> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<String>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 渠道列表
     */
    public Observable<List<ChannelOutput>> getChannelList() {
        return callMapList(service.getChannelList(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<ChannelOutput>>>() {
                    @Override
                    public Observable<List<ChannelOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<ChannelOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<ChannelOutput>>(){});
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 添加结算卡
     */
    public Observable<JSONObject> addjiesuancard(Map<String, String> reqData) {
        return callMap(service.addjiesuancard(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 添加服务商
     */
    public Observable<JSONObject> agentRegisterNew(Map<String, String> reqData) {
        return callMap(service.agentRegisterNew(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }
}
